using System.IO;
using BookingSystem.Entities;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;

namespace BookingSystem
{
    public class Config
    {
        private static readonly string _dbFile = @"BookingSystem.db";

        public static ISessionFactory CreateSessionFactory()
        {
            return Fluently.Configure()
              .Database(
                SQLiteConfiguration.Standard
                  .UsingFile(_dbFile)
              )
              .Mappings(m =>
                m.FluentMappings.AddFromAssemblyOf<Config>())
              .ExposeConfiguration(BuildSchema)
              .BuildSessionFactory();
        }

        private static void BuildSchema(Configuration config)
        {
            // delete the existing db on each run
//            if (File.Exists(_dbFile))
//                File.Delete(_dbFile);

            // this NHibernate tool takes a configuration (with mapping info in)
            // and exports a database schema from it

            if (!File.Exists(_dbFile)) 
                new SchemaExport(config)
              .Create(false, true);
        }
    }
}